home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / hplip / base / utils.py < prev    next >
Encoding:
Python Source  |  2011-12-02  |  49.6 KB  |  1,659 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21. # Thanks to Henrique M. Holschuh <hmh@debian.org> for various security patches
  22. #
  23.  
  24. from __future__ import generators
  25.  
  26. # Std Lib
  27. import sys
  28. import os
  29. import fnmatch
  30. import tempfile
  31. import socket
  32. import struct
  33. import select
  34. import time
  35. import fcntl
  36. import errno
  37. import stat
  38. import string
  39. import commands # TODO: Replace with subprocess (commands is deprecated in Python 3.0)
  40. import cStringIO
  41. import re
  42. import xml.parsers.expat as expat
  43. import getpass
  44. import locale
  45. import htmlentitydefs
  46.  
  47. try:
  48.     import platform
  49.     platform_avail = True
  50. except ImportError:
  51.     platform_avail = False
  52.  
  53. # Local
  54. from g import *
  55. from codes import *
  56. import pexpect
  57.  
  58. BIG_ENDIAN = 0
  59. LITTLE_ENDIAN = 1
  60.  
  61.  
  62.  
  63. def lock(f):
  64.     log.debug("Locking: %s" % f.name)
  65.     try:
  66.         fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
  67.         return True
  68.     except (IOError, OSError):
  69.         log.debug("Failed to unlock %s." % f.name)
  70.         return False
  71.  
  72.  
  73. def unlock(f):
  74.     if f is not None:
  75.         log.debug("Unlocking: %s" % f.name)
  76.         try:
  77.             fcntl.flock(f.fileno(), fcntl.LOCK_UN)
  78.             os.remove(f.name)
  79.         except (IOError, OSError):
  80.             pass
  81.  
  82.  
  83. def lock_app(application, suppress_error=False):
  84.     dir = prop.user_dir
  85.     if os.geteuid() == 0:
  86.         dir = '/var'
  87.  
  88.     elif not os.path.exists(dir):
  89.         os.makedirs(dir)
  90.  
  91.     lock_file = os.path.join(dir, '.'.join([application, 'lock']))
  92.     try:
  93.         lock_file_f = open(lock_file, "w")
  94.     except IOError:
  95.         if not suppress_error:
  96.             log.error("Unable to open %s lock file." % lock_file)
  97.         return False, None
  98.  
  99.     #log.debug("Locking file: %s" % lock_file)
  100.  
  101.     if not lock(lock_file_f):
  102.         if not suppress_error:
  103.             log.error("Unable to lock %s. Is %s already running?" % (lock_file, application))
  104.         return False, None
  105.  
  106.     return True, lock_file_f
  107.  
  108.  
  109. #xml_basename_pat = re.compile(r"""HPLIP-(\d*)_(\d*)_(\d*).xml""", re.IGNORECASE)
  110.  
  111.  
  112. def Translator(frm='', to='', delete='', keep=None):
  113.     allchars = string.maketrans('','')
  114.  
  115.     if len(to) == 1:
  116.         to = to * len(frm)
  117.     trans = string.maketrans(frm, to)
  118.  
  119.     if keep is not None:
  120.         delete = allchars.translate(allchars, keep.translate(allchars, delete))
  121.  
  122.     def callable(s):
  123.         return s.translate(trans, delete)
  124.  
  125.     return callable
  126.  
  127.  
  128. def to_bool_str(s, default='0'):
  129.     """ Convert an arbitrary 0/1/T/F/Y/N string to a normalized string 0/1."""
  130.     if isinstance(s, str) and s:
  131.         if s[0].lower() in ['1', 't', 'y']:
  132.             return u'1'
  133.         elif s[0].lower() in ['0', 'f', 'n']:
  134.             return u'0'
  135.  
  136.     return default
  137.  
  138. def to_bool(s, default=False):
  139.     """ Convert an arbitrary 0/1/T/F/Y/N string to a boolean True/False value."""
  140.     if isinstance(s, str) and s:
  141.         if s[0].lower() in ['1', 't', 'y']:
  142.             return True
  143.         elif s[0].lower() in ['0', 'f', 'n']:
  144.             return False
  145.     elif isinstance(s, bool):
  146.         return s
  147.  
  148.     return default
  149.  
  150.  
  151. def walkFiles(root, recurse=True, abs_paths=False, return_folders=False, pattern='*', path=None):
  152.     if path is None:
  153.         path = root
  154.  
  155.     try:
  156.         names = os.listdir(root)
  157.     except os.error:
  158.         raise StopIteration
  159.  
  160.     pattern = pattern or '*'
  161.     pat_list = pattern.split(';')
  162.  
  163.     for name in names:
  164.         fullname = os.path.normpath(os.path.join(root, name))
  165.  
  166.         for pat in pat_list:
  167.             if fnmatch.fnmatch(name, pat):
  168.                 if return_folders or not os.path.isdir(fullname):
  169.                     if abs_paths:
  170.                         yield fullname
  171.                     else:
  172.                         try:
  173.                             yield os.path.basename(fullname)
  174.                         except ValueError:
  175.                             yield fullname
  176.  
  177.         #if os.path.islink(fullname):
  178.         #    fullname = os.path.realpath(os.readlink(fullname))
  179.  
  180.         if recurse and os.path.isdir(fullname): # or os.path.islink(fullname):
  181.             for f in walkFiles(fullname, recurse, abs_paths, return_folders, pattern, path):
  182.                 yield f
  183.  
  184.  
  185. def is_path_writable(path):
  186.     if os.path.exists(path):
  187.         s = os.stat(path)
  188.         mode = s[stat.ST_MODE] & 0777
  189.  
  190.         if mode & 02:
  191.             return True
  192.         elif s[stat.ST_GID] == os.getgid() and mode & 020:
  193.             return True
  194.         elif s[stat.ST_UID] == os.getuid() and mode & 0200:
  195.             return True
  196.  
  197.     return False
  198.  
  199.  
  200. # Provides the TextFormatter class for formatting text into columns.
  201. # Original Author: Hamish B Lawson, 1999
  202. # Modified by: Don Welch, 2003
  203. class TextFormatter:
  204.  
  205.     LEFT  = 0
  206.     CENTER = 1
  207.     RIGHT  = 2
  208.  
  209.     def __init__(self, colspeclist):
  210.         self.columns = []
  211.         for colspec in colspeclist:
  212.             self.columns.append(Column(**colspec))
  213.  
  214.     def compose(self, textlist, add_newline=False):
  215.         numlines = 0
  216.         textlist = list(textlist)
  217.         if len(textlist) != len(self.columns):
  218.             log.error("Formatter: Number of text items does not match columns")
  219.             return
  220.         for text, column in map(None, textlist, self.columns):
  221.             column.wrap(text)
  222.             numlines = max(numlines, len(column.lines))
  223.         complines = [''] * numlines
  224.         for ln in range(numlines):
  225.             for column in self.columns:
  226.                 complines[ln] = complines[ln] + column.getline(ln)
  227.         if add_newline:
  228.             return '\n'.join(complines) + '\n'
  229.         else:
  230.             return '\n'.join(complines)
  231.  
  232. class Column:
  233.  
  234.     def __init__(self, width=78, alignment=TextFormatter.LEFT, margin=0):
  235.         self.width = width
  236.         self.alignment = alignment
  237.         self.margin = margin
  238.         self.lines = []
  239.  
  240.     def align(self, line):
  241.         if self.alignment == TextFormatter.CENTER:
  242.             return line.center(self.width)
  243.         elif self.alignment == TextFormatter.RIGHT:
  244.             return line.rjust(self.width)
  245.         else:
  246.             return line.ljust(self.width)
  247.  
  248.     def wrap(self, text):
  249.         self.lines = []
  250.         words = []
  251.         for word in text.split():
  252.             if word <= self.width:
  253.                 words.append(word)
  254.             else:
  255.                 for i in range(0, len(word), self.width):
  256.                     words.append(word[i:i+self.width])
  257.         if not len(words): return
  258.         current = words.pop(0)
  259.         for word in words:
  260.             increment = 1 + len(word)
  261.             if len(current) + increment > self.width:
  262.                 self.lines.append(self.align(current))
  263.                 current = word
  264.             else:
  265.                 current = current + ' ' + word
  266.         self.lines.append(self.align(current))
  267.  
  268.     def getline(self, index):
  269.         if index < len(self.lines):
  270.             return ' '*self.margin + self.lines[index]
  271.         else:
  272.             return ' ' * (self.margin + self.width)
  273.  
  274.  
  275.  
  276. class Stack:
  277.     def __init__(self):
  278.         self.stack = []
  279.  
  280.     def pop(self):
  281.         return self.stack.pop()
  282.  
  283.     def push(self, value):
  284.         self.stack.append(value)
  285.  
  286.     def as_list(self):
  287.         return self.stack
  288.  
  289.     def clear(self):
  290.         self.stack = []
  291.  
  292.     def __len__(self):
  293.         return len(self.stack)
  294.  
  295.  
  296.  
  297. class Queue(Stack):
  298.     def __init__(self):
  299.         Stack.__init__(self)
  300.  
  301.     def get(self):
  302.         return self.stack.pop(0)
  303.  
  304.     def put(self, value):
  305.         Stack.push(self, value)
  306.  
  307.  
  308.  
  309. # RingBuffer class
  310. # Source: Python Cookbook 1st Ed., sec. 5.18, pg. 201
  311. # Credit: Sebastien Keim
  312. # License: Modified BSD
  313. class RingBuffer:
  314.     def __init__(self, size_max=50):
  315.         self.max = size_max
  316.         self.data = []
  317.  
  318.     def append(self,x):
  319.         """append an element at the end of the buffer"""
  320.         self.data.append(x)
  321.  
  322.         if len(self.data) == self.max:
  323.             self.cur = 0
  324.             self.__class__ = RingBufferFull
  325.  
  326.     def replace(self, x):
  327.         """replace the last element instead off appending"""
  328.         self.data[-1] = x
  329.  
  330.     def get(self):
  331.         """ return a list of elements from the oldest to the newest"""
  332.         return self.data
  333.  
  334.  
  335. class RingBufferFull:
  336.     def __init__(self, n):
  337.         #raise "you should use RingBuffer"
  338.         pass
  339.  
  340.     def append(self, x):
  341.         self.data[self.cur] = x
  342.         self.cur = (self.cur+1) % self.max
  343.  
  344.     def replace(self, x):
  345.         # back up 1 position to previous location
  346.         self.cur = (self.cur-1) % self.max
  347.         self.data[self.cur] = x
  348.         # setup for next item
  349.         self.cur = (self.cur+1) % self.max
  350.  
  351.     def get(self):
  352.         return self.data[self.cur:] + self.data[:self.cur]
  353.  
  354.  
  355.  
  356. def sort_dict_by_value(d):
  357.     """ Returns the keys of dictionary d sorted by their values """
  358.     items=d.items()
  359.     backitems=[[v[1],v[0]] for v in items]
  360.     backitems.sort()
  361.     return [backitems[i][1] for i in range(0, len(backitems))]
  362.  
  363.  
  364. def commafy(val):
  365.     return locale.format("%d", val, grouping=True).decode(locale.getpreferredencoding())
  366.  
  367.  
  368. def format_bytes(s, show_bytes=False):
  369.     if s < 1024:
  370.         return ''.join([commafy(s), ' B'])
  371.     elif 1024 < s < 1048576:
  372.         if show_bytes:
  373.             return ''.join([unicode(round(s/1024.0, 1)) , u' KB (',  commafy(s), ')'])
  374.         else:
  375.             return ''.join([unicode(round(s/1024.0, 1)) , u' KB'])
  376.     elif 1048576 < s < 1073741824:
  377.         if show_bytes:
  378.             return ''.join([unicode(round(s/1048576.0, 1)), u' MB (',  commafy(s), ')'])
  379.         else:
  380.             return ''.join([unicode(round(s/1048576.0, 1)), u' MB'])
  381.     else:
  382.         if show_bytes:
  383.             return ''.join([unicode(round(s/1073741824.0, 1)), u' GB (',  commafy(s), ')'])
  384.         else:
  385.             return ''.join([unicode(round(s/1073741824.0, 1)), u' GB'])
  386.  
  387.  
  388.  
  389. try:
  390.     make_temp_file = tempfile.mkstemp # 2.3+
  391. except AttributeError:
  392.     def make_temp_file(suffix='', prefix='', dir='', text=False): # pre-2.3
  393.         path = tempfile.mktemp(suffix)
  394.         fd = os.open(path, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
  395.         return ( os.fdopen( fd, 'w+b' ), path )
  396.  
  397.  
  398.  
  399. def which(command, return_full_path=False):
  400.     path = os.getenv('PATH').split(':')
  401.  
  402.     # Add these paths for Fedora
  403.     path.append('/sbin')
  404.     path.append('/usr/sbin')
  405.     path.append('/usr/local/sbin')
  406.  
  407.     found_path = ''
  408.     for p in path:
  409.         try:
  410.             files = os.listdir(p)
  411.         except OSError:
  412.             continue
  413.         else:
  414.             if command in files:
  415.                 found_path = p
  416.                 break
  417.  
  418.     if return_full_path:
  419.         if found_path:
  420.             return os.path.join(found_path, command)
  421.         else:
  422.             return ''
  423.     else:
  424.         return found_path
  425.  
  426.  
  427. class UserSettings(object): # Note: Deprecated after 2.8.8 in Qt4 (see ui4/ui_utils.py)
  428.     def __init__(self):
  429.         self.load()
  430.  
  431.     def loadDefaults(self):
  432.         # Print
  433.         self.cmd_print = ''
  434.         path = which('hp-print')
  435.  
  436.         if len(path) > 0:
  437.             self.cmd_print = 'hp-print -p%PRINTER%'
  438.         else:
  439.             path = which('kprinter')
  440.             if len(path) > 0:
  441.                 self.cmd_print = 'kprinter -P%PRINTER% --system cups'
  442.             else:
  443.                 path = which('gtklp')
  444.                 if len(path) > 0:
  445.                     self.cmd_print = 'gtklp -P%PRINTER%'
  446.                 else:
  447.                     path = which('xpp')
  448.                     if len(path) > 0:
  449.                         self.cmd_print = 'xpp -P%PRINTER%'
  450.  
  451.         # Scan
  452.         self.cmd_scan = ''
  453.         path = which('simple-scan')
  454.         if len(path) > 0:
  455.             self.cmd_scan = 'simple-scan %SANE_URI%'
  456.         else:
  457.             path = which('xsane')
  458.             if len(path) > 0:
  459.                 self.cmd_scan = 'xsane -V %SANE_URI%'
  460.             else:
  461.                 path = which('kooka')
  462.                 if len(path) > 0:
  463.                     self.cmd_scan = 'kooka'
  464.                 else:
  465.                     path = which('xscanimage')
  466.                     if len(path) > 0:
  467.                         self.cmd_scan = 'xscanimage'
  468.  
  469.         # Photo Card
  470.         path = which('hp-unload')
  471.  
  472.         if len(path):
  473.             self.cmd_pcard = 'hp-unload -d %DEVICE_URI%'
  474.         else:
  475.             self.cmd_pcard = 'python %HOME%/unload.py -d %DEVICE_URI%'
  476.  
  477.         # Copy
  478.         path = which('hp-makecopies')
  479.  
  480.         if len(path):
  481.             self.cmd_copy = 'hp-makecopies -d %DEVICE_URI%'
  482.         else:
  483.             self.cmd_copy = 'python %HOME%/makecopies.py -d %DEVICE_URI%'
  484.  
  485.         # Fax
  486.         path = which('hp-sendfax')
  487.  
  488.         if len(path):
  489.             self.cmd_fax = 'hp-sendfax -d %FAX_URI%'
  490.         else:
  491.             self.cmd_fax = 'python %HOME%/sendfax.py -d %FAX_URI%'
  492.  
  493.         # Fax Address Book
  494.         path = which('hp-fab')
  495.  
  496.         if len(path):
  497.             self.cmd_fab = 'hp-fab'
  498.         else:
  499.             self.cmd_fab = 'python %HOME%/fab.py'
  500.  
  501.     def load(self):
  502.         self.loadDefaults()
  503.         log.debug("Loading user settings...")
  504.         self.auto_refresh = to_bool(user_conf.get('refresh', 'enable', '0'))
  505.  
  506.         try:
  507.             self.auto_refresh_rate = int(user_conf.get('refresh', 'rate', '30'))
  508.         except ValueError:
  509.             self.auto_refresh_rate = 30 # (secs)
  510.  
  511.         try:
  512.             self.auto_refresh_type = int(user_conf.get('refresh', 'type', '0'))
  513.         except ValueError:
  514.             self.auto_refresh_type = 0 # refresh 1 (1=refresh all)
  515.  
  516.         self.cmd_print = user_conf.get('commands', 'prnt', self.cmd_print)
  517.         self.cmd_scan = user_conf.get('commands', 'scan', self.cmd_scan)
  518.         self.cmd_pcard = user_conf.get('commands', 'pcard', self.cmd_pcard)
  519.         self.cmd_copy = user_conf.get('commands', 'cpy', self.cmd_copy)
  520.         self.cmd_fax = user_conf.get('commands', 'fax', self.cmd_fax)
  521.         self.cmd_fab = user_conf.get('commands', 'fab', self.cmd_fab)
  522.         self.debug()
  523.  
  524.     def debug(self):
  525.         log.debug("Print command: %s" % self.cmd_print)
  526.         log.debug("PCard command: %s" % self.cmd_pcard)
  527.         log.debug("Fax command: %s" % self.cmd_fax)
  528.         log.debug("FAB command: %s" % self.cmd_fab)
  529.         log.debug("Copy command: %s " % self.cmd_copy)
  530.         log.debug("Scan command: %s" % self.cmd_scan)
  531.         log.debug("Auto refresh: %s" % self.auto_refresh)
  532.         log.debug("Auto refresh rate: %s" % self.auto_refresh_rate)
  533.         log.debug("Auto refresh type: %s" % self.auto_refresh_type)
  534.  
  535.     def save(self):
  536.         log.debug("Saving user settings...")
  537.         user_conf.set('commands', 'prnt', self.cmd_print)
  538.         user_conf.set('commands', 'pcard', self.cmd_pcard)
  539.         user_conf.set('commands', 'fax', self.cmd_fax)
  540.         user_conf.set('commands', 'scan', self.cmd_scan)
  541.         user_conf.set('commands', 'cpy', self.cmd_copy)
  542.         user_conf.set('refresh', 'enable',self.auto_refresh)
  543.         user_conf.set('refresh', 'rate', self.auto_refresh_rate)
  544.         user_conf.set('refresh', 'type', self.auto_refresh_type)
  545.         self.debug()
  546.  
  547.  
  548.  
  549. def no_qt_message_gtk():
  550.     try:
  551.         import gtk
  552.         w = gtk.Window()
  553.         dialog = gtk.MessageDialog(w, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  554.                                    gtk.MESSAGE_WARNING, gtk.BUTTONS_OK,
  555.                                    "PyQt not installed. GUI not available. Please check that the PyQt package is installed. Exiting.")
  556.         dialog.run()
  557.         dialog.destroy()
  558.  
  559.     except ImportError:
  560.         log.error("PyQt not installed. GUI not available. Please check that the PyQt package is installed. Exiting.")
  561.  
  562.  
  563. def canEnterGUIMode(): # qt3
  564.     if not prop.gui_build:
  565.         log.warn("GUI mode disabled in build.")
  566.         return False
  567.  
  568.     elif not os.getenv('DISPLAY'):
  569.         log.warn("No display found.")
  570.         return False
  571.  
  572.     elif not checkPyQtImport():
  573.         log.warn("Qt/PyQt 3 initialization failed.")
  574.         return False
  575.  
  576.     return True
  577.  
  578.  
  579. def canEnterGUIMode4(): # qt4
  580.     if not prop.gui_build:
  581.         log.warn("GUI mode disabled in build.")
  582.         return False
  583.  
  584.     elif not os.getenv('DISPLAY'):
  585.         log.warn("No display found.")
  586.         return False
  587.  
  588.     elif not checkPyQtImport4():
  589.         log.warn("Qt/PyQt 4 initialization failed.")
  590.         return False
  591.  
  592.     return True
  593.  
  594.  
  595. def checkPyQtImport(): # qt3
  596.     # PyQt
  597.     try:
  598.         import qt
  599.     except ImportError:
  600.         if os.getenv('DISPLAY') and os.getenv('STARTED_FROM_MENU'):
  601.             no_qt_message_gtk()
  602.  
  603.         log.error("PyQt not installed. GUI not available. Exiting.")
  604.         return False
  605.  
  606.     # check version of Qt
  607.     qtMajor = int(qt.qVersion().split('.')[0])
  608.  
  609.     if qtMajor < MINIMUM_QT_MAJOR_VER:
  610.  
  611.         log.error("Incorrect version of Qt installed. Ver. 3.0.0 or greater required.")
  612.         return False
  613.  
  614.     #check version of PyQt
  615.     try:
  616.         pyqtVersion = qt.PYQT_VERSION_STR
  617.     except AttributeError:
  618.         pyqtVersion = qt.PYQT_VERSION
  619.  
  620.     while pyqtVersion.count('.') < 2:
  621.         pyqtVersion += '.0'
  622.  
  623.     (maj_ver, min_ver, pat_ver) = pyqtVersion.split('.')
  624.  
  625.     if pyqtVersion.find('snapshot') >= 0:
  626.         log.warning("A non-stable snapshot version of PyQt is installed.")
  627.     else:
  628.         try:
  629.             maj_ver = int(maj_ver)
  630.             min_ver = int(min_ver)
  631.             pat_ver = int(pat_ver)
  632.         except ValueError:
  633.             maj_ver, min_ver, pat_ver = 0, 0, 0
  634.  
  635.         if maj_ver < MINIMUM_PYQT_MAJOR_VER or \
  636.             (maj_ver == MINIMUM_PYQT_MAJOR_VER and min_ver < MINIMUM_PYQT_MINOR_VER):
  637.             log.error("This program may not function properly with the version of PyQt that is installed (%d.%d.%d)." % (maj_ver, min_ver, pat_ver))
  638.             log.error("Incorrect version of pyQt installed. Ver. %d.%d or greater required." % (MINIMUM_PYQT_MAJOR_VER, MINIMUM_PYQT_MINOR_VER))
  639.             log.error("This program will continue, but you may experience errors, crashes or other problems.")
  640.             return True
  641.  
  642.     return True
  643.  
  644.  
  645. def checkPyQtImport4():
  646.     try:
  647.         import PyQt4
  648.     except ImportError:
  649.         return False
  650.     else:
  651.         return True
  652.  
  653.  
  654. try:
  655.     from string import Template # will fail in Python <= 2.3
  656. except ImportError:
  657.     # Code from Python 2.4 string.py
  658.     #import re as _re
  659.  
  660.     class _multimap:
  661.         """Helper class for combining multiple mappings.
  662.  
  663.         Used by .{safe_,}substitute() to combine the mapping and keyword
  664.         arguments.
  665.         """
  666.         def __init__(self, primary, secondary):
  667.             self._primary = primary
  668.             self._secondary = secondary
  669.  
  670.         def __getitem__(self, key):
  671.             try:
  672.                 return self._primary[key]
  673.             except KeyError:
  674.                 return self._secondary[key]
  675.  
  676.  
  677.     class _TemplateMetaclass(type):
  678.         pattern = r"""
  679.         %(delim)s(?:
  680.           (?P<escaped>%(delim)s) |   # Escape sequence of two delimiters
  681.           (?P<named>%(id)s)      |   # delimiter and a Python identifier
  682.           {(?P<braced>%(id)s)}   |   # delimiter and a braced identifier
  683.           (?P<invalid>)              # Other ill-formed delimiter exprs
  684.         )
  685.         """
  686.  
  687.         def __init__(cls, name, bases, dct):
  688.             super(_TemplateMetaclass, cls).__init__(name, bases, dct)
  689.             if 'pattern' in dct:
  690.                 pattern = cls.pattern
  691.             else:
  692.                 pattern = _TemplateMetaclass.pattern % {
  693.                     'delim' : re.escape(cls.delimiter),
  694.                     'id'    : cls.idpattern,
  695.                     }
  696.             cls.pattern = re.compile(pattern, re.IGNORECASE | re.VERBOSE)
  697.  
  698.  
  699.     class Template:
  700.         """A string class for supporting $-substitutions."""
  701.         __metaclass__ = _TemplateMetaclass
  702.  
  703.         delimiter = '$'
  704.         idpattern = r'[_a-z][_a-z0-9]*'
  705.  
  706.         def __init__(self, template):
  707.             self.template = template
  708.  
  709.         # Search for $$, $identifier, ${identifier}, and any bare $'s
  710.         def _invalid(self, mo):
  711.             i = mo.start('invalid')
  712.             lines = self.template[:i].splitlines(True)
  713.             if not lines:
  714.                 colno = 1
  715.                 lineno = 1
  716.             else:
  717.                 colno = i - len(''.join(lines[:-1]))
  718.                 lineno = len(lines)
  719.             raise ValueError('Invalid placeholder in string: line %d, col %d' %
  720.                              (lineno, colno))
  721.  
  722.         def substitute(self, *args, **kws):
  723.             if len(args) > 1:
  724.                 raise TypeError('Too many positional arguments')
  725.             if not args:
  726.                 mapping = kws
  727.             elif kws:
  728.                 mapping = _multimap(kws, args[0])
  729.             else:
  730.                 mapping = args[0]
  731.             # Helper function for .sub()
  732.             def convert(mo):
  733.                 # Check the most common path first.
  734.                 named = mo.group('named') or mo.group('braced')
  735.                 if named is not None:
  736.                     val = mapping[named]
  737.                     # We use this idiom instead of str() because the latter will
  738.                     # fail if val is a Unicode containing non-ASCII characters.
  739.                     return '%s' % val
  740.                 if mo.group('escaped') is not None:
  741.                     return self.delimiter
  742.                 if mo.group('invalid') is not None:
  743.                     self._invalid(mo)
  744.                 raise ValueError('Unrecognized named group in pattern',
  745.                                  self.pattern)
  746.             return self.pattern.sub(convert, self.template)
  747.  
  748.  
  749.         def safe_substitute(self, *args, **kws):
  750.             if len(args) > 1:
  751.                 raise TypeError('Too many positional arguments')
  752.             if not args:
  753.                 mapping = kws
  754.             elif kws:
  755.                 mapping = _multimap(kws, args[0])
  756.             else:
  757.                 mapping = args[0]
  758.             # Helper function for .sub()
  759.             def convert(mo):
  760.                 named = mo.group('named')
  761.                 if named is not None:
  762.                     try:
  763.                         # We use this idiom instead of str() because the latter
  764.                         # will fail if val is a Unicode containing non-ASCII
  765.                         return '%s' % mapping[named]
  766.                     except KeyError:
  767.                         return self.delimiter + named
  768.                 braced = mo.group('braced')
  769.                 if braced is not None:
  770.                     try:
  771.                         return '%s' % mapping[braced]
  772.                     except KeyError:
  773.                         return self.delimiter + '{' + braced + '}'
  774.                 if mo.group('escaped') is not None:
  775.                     return self.delimiter
  776.                 if mo.group('invalid') is not None:
  777.                     return self.delimiter
  778.                 raise ValueError('Unrecognized named group in pattern',
  779.                                  self.pattern)
  780.             return self.pattern.sub(convert, self.template)
  781.  
  782.  
  783.  
  784. #cat = lambda _ : Template(_).substitute(sys._getframe(1).f_globals, **sys._getframe(1).f_locals)
  785.  
  786. def cat(s):
  787.     globals = sys._getframe(1).f_globals.copy()
  788.     if 'self' in globals:
  789.         del globals['self']
  790.  
  791.     locals = sys._getframe(1).f_locals.copy()
  792.     if 'self' in locals:
  793.         del locals['self']
  794.  
  795.     return Template(s).substitute(sys._getframe(1).f_globals, **locals)
  796.  
  797.  
  798. identity = string.maketrans('','')
  799. unprintable = identity.translate(identity, string.printable)
  800.  
  801.  
  802. def printable(s):
  803.     return s.translate(identity, unprintable)
  804.  
  805.  
  806. def any(S,f=lambda x:x):
  807.     for x in S:
  808.         if f(x): return True
  809.     return False
  810.  
  811.  
  812. def all(S,f=lambda x:x):
  813.     for x in S:
  814.         if not f(x): return False
  815.     return True
  816.  
  817.  
  818. BROWSERS = ['firefox', 'mozilla', 'konqueror', 'galeon', 'skipstone'] # in preferred order
  819. BROWSER_OPTS = {'firefox': '-new-window', 'mozilla' : '', 'konqueror': '', 'galeon': '-w', 'skipstone': ''}
  820.  
  821.  
  822. def find_browser():
  823.     if platform_avail and platform.system() == 'Darwin':
  824.         return "open"
  825.     else:
  826.         for b in BROWSERS:
  827.             if which(b):
  828.                 return b
  829.         else:
  830.             return None
  831.  
  832.  
  833. def openURL(url, use_browser_opts=True):
  834.     if platform_avail and platform.system() == 'Darwin':
  835.         cmd = 'open "%s"' % url
  836.         log.debug(cmd)
  837.         os.system(cmd)
  838.     else:
  839.         for b in BROWSERS:
  840.             bb = which(b)
  841.             if bb:
  842.                 bb = os.path.join(bb, b)
  843.                 if use_browser_opts:
  844.                     cmd = """%s %s "%s" &""" % (bb, BROWSER_OPTS[b], url)
  845.                 else:
  846.                     cmd = """%s "%s" &""" % (bb, url)
  847.                 log.debug(cmd)
  848.                 os.system(cmd)
  849.                 break
  850.         else:
  851.             log.warn("Unable to open URL: %s" % url)
  852.  
  853.  
  854. def uniqueList(input):
  855.     temp = []
  856.     [temp.append(i) for i in input if not temp.count(i)]
  857.     return temp
  858.  
  859.  
  860. def list_move_up(l, m, cmp=None):
  861.     if cmp is None:
  862.         f = lambda x: l[x] == m
  863.     else:
  864.         f = lambda x: cmp(l[x], m)
  865.  
  866.     for i in range(1, len(l)):
  867.         if f(i):
  868.             l[i-1], l[i] = l[i], l[i-1]
  869.  
  870.  
  871. def list_move_down(l, m, cmp=None):
  872.     if cmp is None:
  873.         f = lambda x: l[x] == m
  874.     else:
  875.         f = lambda x: cmp(l[x], m)
  876.  
  877.     for i in range(len(l)-2, -1, -1):
  878.         if f(i):
  879.             l[i], l[i+1] = l[i+1], l[i]
  880.  
  881.  
  882.  
  883. class XMLToDictParser:
  884.     def __init__(self):
  885.         self.stack = []
  886.         self.data = {}
  887.         self.last_start = ''
  888.  
  889.     def startElement(self, name, attrs):
  890.         #print "START:", name, attrs
  891.         self.stack.append(unicode(name).lower())
  892.         self.last_start = unicode(name).lower()
  893.  
  894.         if len(attrs):
  895.             for a in attrs:
  896.                 self.stack.append(unicode(a).lower())
  897.                 self.addData(attrs[a])
  898.                 self.stack.pop()
  899.  
  900.     def endElement(self, name):
  901.         if name.lower() == self.last_start:
  902.             self.addData('')
  903.  
  904.         #print "END:", name
  905.         self.stack.pop()
  906.  
  907.     def charData(self, data):
  908.         data = unicode(data).strip()
  909.  
  910.         if data and self.stack:
  911.             self.addData(data)
  912.  
  913.     def addData(self, data):
  914.         #print "DATA:", data
  915.         self.last_start = ''
  916.         try:
  917.             data = int(data)
  918.         except ValueError:
  919.             data = unicode(data)
  920.  
  921.         stack_str = '-'.join(self.stack)
  922.         stack_str_0 = '-'.join([stack_str, '0'])
  923.  
  924.         try:
  925.             self.data[stack_str]
  926.         except KeyError:
  927.             try:
  928.                 self.data[stack_str_0]
  929.             except KeyError:
  930.                 self.data[stack_str] = data
  931.             else:
  932.                 j = 2
  933.                 while True:
  934.                     try:
  935.                         self.data['-'.join([stack_str, unicode(j)])]
  936.                     except KeyError:
  937.                         self.data['-'.join([stack_str, unicode(j)])] = data
  938.                         break
  939.                     j += 1
  940.  
  941.         else:
  942.             self.data[stack_str_0] = self.data[stack_str]
  943.             self.data['-'.join([stack_str, '1'])] = data
  944.             del self.data[stack_str]
  945.  
  946.  
  947.     def parseXML(self, text):
  948.         parser = expat.ParserCreate()
  949.         parser.StartElementHandler = self.startElement
  950.         parser.EndElementHandler = self.endElement
  951.         parser.CharacterDataHandler = self.charData
  952.         parser.Parse(text.encode('utf-8'), True)
  953.         return self.data
  954.  
  955.  
  956. def dquote(s):
  957.     return ''.join(['"', s, '"'])
  958.  
  959.  
  960. # Python 2.2.x compatibility functions (strip() family with char argument added in Python 2.2.3)
  961. if sys.hexversion < 0x020203f0:
  962.     def xlstrip(s, chars=' '):
  963.         i = 0
  964.         for c, i in zip(s, range(len(s))):
  965.             if c not in chars:
  966.                 break
  967.  
  968.         return s[i:]
  969.  
  970.     def xrstrip(s, chars=' '):
  971.         return xreverse(xlstrip(xreverse(s), chars))
  972.  
  973.     def xreverse(s):
  974.         l = list(s)
  975.         l.reverse()
  976.         return ''.join(l)
  977.  
  978.     def xstrip(s, chars=' '):
  979.         return xreverse(xlstrip(xreverse(xlstrip(s, chars)), chars))
  980.  
  981. else:
  982.     xlstrip = string.lstrip
  983.     xrstrip = string.rstrip
  984.     xstrip = string.strip
  985.  
  986.  
  987. def getBitness():
  988.     if platform_avail:
  989.         return int(platform.architecture()[0][:-3])
  990.     else:
  991.         return struct.calcsize("P") << 3
  992.  
  993.  
  994. def getProcessor():
  995.     if platform_avail:
  996.         return platform.machine().replace(' ', '_').lower() # i386, i686, power_macintosh, etc.
  997.     else:
  998.         return "i686" # TODO: Need a fix here
  999.  
  1000.  
  1001. def getEndian():
  1002.     if sys.byteorder == 'big':
  1003.         return BIG_ENDIAN
  1004.     else:
  1005.         return LITTLE_ENDIAN
  1006.  
  1007.  
  1008. def get_password():
  1009.     return getpass.getpass("Enter password: ")
  1010.  
  1011. def get_password_ui():
  1012.     fp = open("/etc/hp/hplip.conf", "r")
  1013.     qt = "qt3"
  1014.     for line in fp:
  1015.         if string.find(line, "qt4") is not -1 and string.find(line, "yes") is not -1:
  1016.             qt = "qt4"
  1017.     fp.close()
  1018.     if qt is "qt4":
  1019.         from ui4.setupdialog import showPasswordUI
  1020.         username, password = showPasswordUI("Your printer requires to install HP proprietary plugin\nPlease enter root/superuser password to continue")
  1021.     if qt is "qt3":
  1022.         from ui.setupform import showPasswordUI
  1023.         username, password = showPasswordUI("Your priter requires to install HP proprietary plugin\nPlease enter root/superuser password to continue")
  1024.     return password
  1025.  
  1026. def run(cmd, log_output=True, password_func=get_password, timeout=1):
  1027.     output = cStringIO.StringIO()
  1028.  
  1029.     try:
  1030.         child = pexpect.spawn(cmd, timeout=timeout)
  1031.     except pexpect.ExceptionPexpect:
  1032.         return -1, ''
  1033.  
  1034.     try:
  1035.         while True:
  1036.             update_spinner()
  1037.             i = child.expect(["[pP]assword:", pexpect.EOF, pexpect.TIMEOUT])
  1038.  
  1039.             if child.before:
  1040.                 output.write(child.before)
  1041.                 if log_output:
  1042.                     log.debug(child.before)
  1043.  
  1044.             if i == 0: # Password:
  1045.                 if password_func is not None:
  1046.                     if password_func == "get_password_ui":
  1047.                         child.sendline(get_password_ui())
  1048.                     else:
  1049.                         child.sendline(password_func())
  1050.                 else:
  1051.                     child.sendline(get_password())
  1052.  
  1053.             elif i == 1: # EOF
  1054.                 break
  1055.  
  1056.             elif i == 2: # TIMEOUT
  1057.                 continue
  1058.  
  1059.  
  1060.     except Exception, e:
  1061.         log.error("Exception: %s" % e)
  1062.  
  1063.     cleanup_spinner()
  1064.     child.close()
  1065.  
  1066.     return child.exitstatus, output.getvalue()
  1067.  
  1068.  
  1069. def expand_range(ns): # ns -> string repr. of numeric range, e.g. "1-4, 7, 9-12"
  1070.     """Credit: Jean Brouwers, comp.lang.python 16-7-2004
  1071.        Convert a string representation of a set of ranges into a
  1072.        list of ints, e.g.
  1073.        u"1-4, 7, 9-12" --> [1,2,3,4,7,9,10,11,12]
  1074.     """
  1075.     fs = []
  1076.     for n in ns.split(u','):
  1077.         n = n.strip()
  1078.         r = n.split('-')
  1079.         if len(r) == 2:  # expand name with range
  1080.             h = r[0].rstrip(u'0123456789')  # header
  1081.             r[0] = r[0][len(h):]
  1082.              # range can't be empty
  1083.             if not (r[0] and r[1]):
  1084.                 raise ValueError, 'empty range: ' + n
  1085.              # handle leading zeros
  1086.             if r[0] == u'0' or r[0][0] != u'0':
  1087.                 h += '%d'
  1088.             else:
  1089.                 w = [len(i) for i in r]
  1090.                 if w[1] > w[0]:
  1091.                    raise ValueError, 'wide range: ' + n
  1092.                 h += u'%%0%dd' % max(w)
  1093.              # check range
  1094.             r = [int(i, 10) for i in r]
  1095.             if r[0] > r[1]:
  1096.                raise ValueError, 'bad range: ' + n
  1097.             for i in range(r[0], r[1]+1):
  1098.                 fs.append(h % i)
  1099.         else:  # simple name
  1100.             fs.append(n)
  1101.  
  1102.      # remove duplicates
  1103.     fs = dict([(n, i) for i, n in enumerate(fs)]).keys()
  1104.      # convert to ints and sort
  1105.     fs = [int(x) for x in fs if x]
  1106.     fs.sort()
  1107.  
  1108.     return fs
  1109.  
  1110.  
  1111. def collapse_range(x): # x --> sorted list of ints
  1112.     """ Convert a list of integers into a string
  1113.         range representation:
  1114.         [1,2,3,4,7,9,10,11,12] --> u"1-4,7,9-12"
  1115.     """
  1116.     if not x:
  1117.         return ''
  1118.  
  1119.     s, c, r = [str(x[0])], x[0], False
  1120.  
  1121.     for i in x[1:]:
  1122.         if i == (c+1):
  1123.             r = True
  1124.         else:
  1125.             if r:
  1126.                 s.append(u'-%s,%s' % (c,i))
  1127.                 r = False
  1128.             else:
  1129.                 s.append(u',%s' % i)
  1130.  
  1131.         c = i
  1132.  
  1133.     if r:
  1134.         s.append(u'-%s' % i)
  1135.  
  1136.     return ''.join(s)
  1137.  
  1138.  
  1139. def createSequencedFilename(basename, ext, dir=None, digits=3):
  1140.     if dir is None:
  1141.         dir = os.getcwd()
  1142.  
  1143.     m = 0
  1144.     for f in walkFiles(dir, recurse=False, abs_paths=False, return_folders=False, pattern='*', path=None):
  1145.         r, e = os.path.splitext(f)
  1146.  
  1147.         if r.startswith(basename) and ext == e:
  1148.             try:
  1149.                 i = int(r[len(basename):])
  1150.             except ValueError:
  1151.                 continue
  1152.             else:
  1153.                 m = max(m, i)
  1154.  
  1155.     return os.path.join(dir, "%s%0*d%s" % (basename, digits, m+1, ext))
  1156.  
  1157.  
  1158. def validate_language(lang, default='en_US'):
  1159.     if lang is None:
  1160.         loc, encoder = locale.getdefaultlocale()
  1161.     else:
  1162.         lang = lang.lower().strip()
  1163.         for loc, ll in supported_locales.items():
  1164.             if lang in ll:
  1165.                 break
  1166.         else:
  1167.             loc = 'en_US'
  1168.             log.warn("Unknown lang/locale. Using default of %s." % loc)
  1169.  
  1170.     return loc
  1171.  
  1172.  
  1173. def gen_random_uuid():
  1174.     try:
  1175.         import uuid # requires Python 2.5+
  1176.         return str(uuid.uuid4())
  1177.  
  1178.     except ImportError:
  1179.         uuidgen = which("uuidgen")
  1180.         if uuidgen:
  1181.             uuidgen = os.path.join(uuidgen, "uuidgen")
  1182.             return commands.getoutput(uuidgen) # TODO: Replace with subprocess (commands is deprecated in Python 3.0)
  1183.         else:
  1184.             return ''
  1185.  
  1186.  
  1187. class RestTableFormatter(object):
  1188.     def __init__(self, header=None):
  1189.         self.header = header # tuple of strings
  1190.         self.rows = [] # list of tuples
  1191.  
  1192.     def add(self, row_data): # tuple of strings
  1193.         self.rows.append(row_data)
  1194.  
  1195.     def output(self, w):
  1196.         if self.rows:
  1197.             num_cols = len(self.rows[0])
  1198.             for r in self.rows:
  1199.                 if len(r) != num_cols:
  1200.                     log.error("Invalid number of items in row: %s" % r)
  1201.                     return
  1202.  
  1203.             if len(self.header) != num_cols:
  1204.                 log.error("Invalid number of items in header.")
  1205.  
  1206.             col_widths = []
  1207.             for x, c in enumerate(self.header):
  1208.                 max_width = len(c)
  1209.                 for r in self.rows:
  1210.                     max_width = max(max_width, len(r[x]))
  1211.  
  1212.                 col_widths.append(max_width+2)
  1213.  
  1214.             x = '+'
  1215.             for c in col_widths:
  1216.                 x = ''.join([x, '-' * (c+2), '+'])
  1217.  
  1218.             x = ''.join([x, '\n'])
  1219.             w.write(x)
  1220.  
  1221.             # header
  1222.             if self.header:
  1223.                 x = '|'
  1224.                 for i, c in enumerate(col_widths):
  1225.                     x = ''.join([x, ' ', self.header[i], ' ' * (c+1-len(self.header[i])), '|'])
  1226.  
  1227.                 x = ''.join([x, '\n'])
  1228.                 w.write(x)
  1229.  
  1230.                 x = '+'
  1231.                 for c in col_widths:
  1232.                     x = ''.join([x, '=' * (c+2), '+'])
  1233.  
  1234.                 x = ''.join([x, '\n'])
  1235.                 w.write(x)
  1236.  
  1237.             # data rows
  1238.             for j, r in enumerate(self.rows):
  1239.                 x = '|'
  1240.                 for i, c in enumerate(col_widths):
  1241.                     x = ''.join([x, ' ', self.rows[j][i], ' ' * (c+1-len(self.rows[j][i])), '|'])
  1242.  
  1243.                 x = ''.join([x, '\n'])
  1244.                 w.write(x)
  1245.  
  1246.                 x = '+'
  1247.                 for c in col_widths:
  1248.                     x = ''.join([x, '-' * (c+2), '+'])
  1249.  
  1250.                 x = ''.join([x, '\n'])
  1251.                 w.write(x)
  1252.  
  1253.         else:
  1254.             log.error("No data rows")
  1255.  
  1256.  
  1257. def mixin(cls):
  1258.     import inspect
  1259.  
  1260.     locals = inspect.stack()[1][0].f_locals
  1261.     if "__module__" not in locals:
  1262.         raise TypeError("Must call mixin() from within class def.")
  1263.  
  1264.     dict = cls.__dict__.copy()
  1265.     dict.pop("__doc__", None)
  1266.     dict.pop("__module__", None)
  1267.  
  1268.     locals.update(dict)
  1269.  
  1270.  
  1271.  
  1272. # TODO: Move usage stuff to to base/module/Module class
  1273.  
  1274.  
  1275.  # ------------------------- Usage Help
  1276. USAGE_OPTIONS = ("[OPTIONS]", "", "heading", False)
  1277. USAGE_LOGGING1 = ("Set the logging level:", "-l<level> or --logging=<level>", 'option', False)
  1278. USAGE_LOGGING2 = ("", "<level>: none, info\*, error, warn, debug (\*default)", "option", False)
  1279. USAGE_LOGGING3 = ("Run in debug mode:", "-g (same as option: -ldebug)", "option", False)
  1280. USAGE_LOGGING_PLAIN = ("Output plain text only:", "-t", "option", False)
  1281. USAGE_ARGS = ("[PRINTER|DEVICE-URI]", "", "heading", False)
  1282. USAGE_ARGS2 = ("[PRINTER]", "", "heading", False)
  1283. USAGE_DEVICE = ("To specify a device-URI:", "-d<device-uri> or --device=<device-uri>", "option", False)
  1284. USAGE_PRINTER = ("To specify a CUPS printer:", "-p<printer> or --printer=<printer>", "option", False)
  1285. USAGE_BUS1 = ("Bus to probe (if device not specified):", "-b<bus> or --bus=<bus>", "option", False)
  1286. USAGE_BUS2 = ("", "<bus>: cups\*, usb\*, net, bt, fw, par\* (\*defaults) (Note: bt and fw not supported in this release.)", 'option', False)
  1287. USAGE_HELP = ("This help information:", "-h or --help", "option", True)
  1288. USAGE_SPACE = ("", "", "space", False)
  1289. USAGE_EXAMPLES = ("Examples:", "", "heading", False)
  1290. USAGE_NOTES = ("Notes:", "", "heading", False)
  1291. USAGE_STD_NOTES1 = ("If device or printer is not specified, the local device bus is probed and the program enters interactive mode.", "", "note", False)
  1292. USAGE_STD_NOTES2 = ("If -p\* is specified, the default CUPS printer will be used.", "", "note", False)
  1293. USAGE_SEEALSO = ("See Also:", "", "heading", False)
  1294. USAGE_LANGUAGE = ("Set the language:", "-q <lang> or --lang=<lang>. Use -q? or --lang=? to see a list of available language codes.", "option", False)
  1295. USAGE_LANGUAGE2 = ("Set the language:", "--lang=<lang>. Use --lang=? to see a list of available language codes.", "option", False)
  1296. USAGE_MODE = ("[MODE]", "", "header", False)
  1297. USAGE_NON_INTERACTIVE_MODE = ("Run in non-interactive mode:", "-n or --non-interactive", "option", False)
  1298. USAGE_GUI_MODE = ("Run in graphical UI mode:", "-u or --gui (Default)", "option", False)
  1299. USAGE_INTERACTIVE_MODE = ("Run in interactive mode:", "-i or --interactive", "option", False)
  1300.  
  1301. if sys_conf.get('configure', 'ui-toolkit', 'qt3') == 'qt3':
  1302.     USAGE_USE_QT3 = ("Use Qt3:",  "--qt3 (Default)",  "option",  False)
  1303.     USAGE_USE_QT4 = ("Use Qt4:",  "--qt4",  "option",  False)
  1304. else:
  1305.     USAGE_USE_QT3 = ("Use Qt3:",  "--qt3",  "option",  False)
  1306.     USAGE_USE_QT4 = ("Use Qt4:",  "--qt4 (Default)",  "option",  False)
  1307.  
  1308.  
  1309.  
  1310.  
  1311. def ttysize(): # TODO: Move to base/tui
  1312.     ln1 = commands.getoutput('stty -a').splitlines()[0]
  1313.     vals = {'rows':None, 'columns':None}
  1314.     for ph in ln1.split(';'):
  1315.         x = ph.split()
  1316.         if len(x) == 2:
  1317.             vals[x[0]] = x[1]
  1318.             vals[x[1]] = x[0]
  1319.     try:
  1320.         rows, cols = int(vals['rows']), int(vals['columns'])
  1321.     except TypeError:
  1322.         rows, cols = 25, 80
  1323.  
  1324.     return rows, cols
  1325.  
  1326.  
  1327. def usage_formatter(override=0): # TODO: Move to base/module/Module class
  1328.     rows, cols = ttysize()
  1329.  
  1330.     if override:
  1331.         col1 = override
  1332.         col2 = cols - col1 - 8
  1333.     else:
  1334.         col1 = int(cols / 3) - 8
  1335.         col2 = cols - col1 - 8
  1336.  
  1337.     return TextFormatter(({'width': col1, 'margin' : 2},
  1338.                             {'width': col2, 'margin' : 2},))
  1339.  
  1340.  
  1341. def format_text(text_list, typ='text', title='', crumb='', version=''): # TODO: Move to base/module/Module class
  1342.     """
  1343.     Format usage text in multiple formats:
  1344.         text: for --help in the console
  1345.         rest: for conversion with rst2web for the website
  1346.         man: for manpages
  1347.     """
  1348.     if typ == 'text':
  1349.         formatter = usage_formatter()
  1350.  
  1351.         for line in text_list:
  1352.             text1, text2, format, trailing_space = line
  1353.  
  1354.             # remove any reST/man escapes
  1355.             text1 = text1.replace("\\", "")
  1356.             text2 = text2.replace("\\", "")
  1357.  
  1358.             if format == 'summary':
  1359.                 log.info(log.bold(text1))
  1360.                 log.info("")
  1361.  
  1362.             elif format in ('para', 'name', 'seealso'):
  1363.                 log.info(text1)
  1364.  
  1365.                 if trailing_space:
  1366.                     log.info("")
  1367.  
  1368.             elif format in ('heading', 'header'):
  1369.                 log.info(log.bold(text1))
  1370.  
  1371.             elif format in ('option', 'example'):
  1372.                 log.info(formatter.compose((text1, text2), trailing_space))
  1373.  
  1374.             elif format == 'note':
  1375.                 if text1.startswith(' '):
  1376.                     log.info('\t' + text1.lstrip())
  1377.                 else:
  1378.                     log.info(text1)
  1379.  
  1380.             elif format == 'space':
  1381.                 log.info("")
  1382.  
  1383.         log.info("")
  1384.  
  1385.  
  1386.     elif typ == 'rest':
  1387.         opt_colwidth1, opt_colwidth2 = 0, 0
  1388.         exmpl_colwidth1, exmpl_colwidth2 = 0, 0
  1389.         note_colwidth1, note_colwidth2 = 0, 0
  1390.  
  1391.         for line in text_list:
  1392.             text1, text2, format, trailing_space = line
  1393.  
  1394.             if format  == 'option':
  1395.                 opt_colwidth1 = max(len(text1), opt_colwidth1)
  1396.                 opt_colwidth2 = max(len(text2), opt_colwidth2)
  1397.  
  1398.             elif format == 'example':
  1399.                 exmpl_colwidth1 = max(len(text1), exmpl_colwidth1)
  1400.                 exmpl_colwidth2 = max(len(text2), exmpl_colwidth2)
  1401.  
  1402.             elif format == 'note':
  1403.                 note_colwidth1 = max(len(text1), note_colwidth1)
  1404.                 note_colwidth2 = max(len(text2), note_colwidth2)
  1405.  
  1406.         opt_colwidth1 += 4
  1407.         opt_colwidth2 += 4
  1408.         exmpl_colwidth1 += 4
  1409.         exmpl_colwidth2 += 4
  1410.         note_colwidth1 += 4
  1411.         note_colwidth2 += 4
  1412.         opt_tablewidth = opt_colwidth1 + opt_colwidth2
  1413.         exmpl_tablewidth = exmpl_colwidth1 + exmpl_colwidth2
  1414.         note_tablewidth = note_colwidth1 + note_colwidth2
  1415.  
  1416.         # write the rst2web header
  1417.         log.info("""restindex
  1418. page-title: %s
  1419. crumb: %s
  1420. format: rest
  1421. file-extension: html
  1422. encoding: utf8
  1423. /restindex\n""" % (title, crumb))
  1424.  
  1425.         t = "%s: %s (ver. %s)" % (crumb, title, version)
  1426.         log.info(t)
  1427.         log.info("="*len(t))
  1428.         log.info("")
  1429.  
  1430.         links = []
  1431.         needs_header = False
  1432.         for line in text_list:
  1433.             text1, text2, format, trailing_space = line
  1434.  
  1435.             if format == 'seealso':
  1436.                 links.append(text1)
  1437.                 text1 = "`%s`_" % text1
  1438.  
  1439.             len1, len2 = len(text1), len(text2)
  1440.  
  1441.             if format == 'summary':
  1442.                 log.info(''.join(["**", text1, "**"]))
  1443.                 log.info("")
  1444.  
  1445.             elif format in ('para', 'name'):
  1446.                 log.info("")
  1447.                 log.info(text1)
  1448.                 log.info("")
  1449.  
  1450.             elif format in ('heading', 'header'):
  1451.  
  1452.                 log.info("")
  1453.                 log.info("**" + text1 + "**")
  1454.                 log.info("")
  1455.                 needs_header = True
  1456.  
  1457.             elif format == 'option':
  1458.                 if needs_header:
  1459.                     log.info(".. class:: borderless")
  1460.                     log.info("")
  1461.                     log.info(''.join(["+", "-"*opt_colwidth1, "+", "-"*opt_colwidth2, "+"]))
  1462.                     needs_header = False
  1463.  
  1464.                 if text1 and '`_' not in text1:
  1465.                     log.info(''.join(["| *", text1, '*', " "*(opt_colwidth1-len1-3), "|", text2, " "*(opt_colwidth2-len2), "|"]))
  1466.                 elif text1:
  1467.                     log.info(''.join(["|", text1, " "*(opt_colwidth1-len1), "|", text2, " "*(opt_colwidth2-len2), "|"]))
  1468.                 else:
  1469.                     log.info(''.join(["|", " "*(opt_colwidth1), "|", text2, " "*(opt_colwidth2-len2), "|"]))
  1470.  
  1471.                 log.info(''.join(["+", "-"*opt_colwidth1, "+", "-"*opt_colwidth2, "+"]))
  1472.  
  1473.             elif format == 'example':
  1474.                 if needs_header:
  1475.                     log.info(".. class:: borderless")
  1476.                     log.info("")
  1477.                     log.info(''.join(["+", "-"*exmpl_colwidth1, "+", "-"*exmpl_colwidth2, "+"]))
  1478.                     needs_header = False
  1479.  
  1480.                 if text1 and '`_' not in text1:
  1481.                     log.info(''.join(["| *", text1, '*', " "*(exmpl_colwidth1-len1-3), "|", text2, " "*(exmpl_colwidth2-len2), "|"]))
  1482.                 elif text1:
  1483.                     log.info(''.join(["|", text1, " "*(exmpl_colwidth1-len1), "|", text2, " "*(exmpl_colwidth2-len2), "|"]))
  1484.                 else:
  1485.                     log.info(''.join(["|", " "*(exmpl_colwidth1), "|", text2, " "*(exmpl_colwidth2-len2), "|"]))
  1486.  
  1487.                 log.info(''.join(["+", "-"*exmpl_colwidth1, "+", "-"*exmpl_colwidth2, "+"]))
  1488.  
  1489.             elif format == 'seealso':
  1490.                 if text1 and '`_' not in text1:
  1491.                     log.info(text1)
  1492.  
  1493.  
  1494.             elif format == 'note':
  1495.                 if needs_header:
  1496.                     log.info(".. class:: borderless")
  1497.                     log.info("")
  1498.                     log.info(''.join(["+", "-"*note_colwidth1, "+", "-"*note_colwidth2, "+"]))
  1499.                     needs_header = False
  1500.  
  1501.                 if text1.startswith(' '):
  1502.                     log.info(''.join(["|", " "*(note_tablewidth+1), "|"]))
  1503.  
  1504.                 log.info(''.join(["|", text1, " "*(note_tablewidth-len1+1), "|"]))
  1505.                 log.info(''.join(["+", "-"*note_colwidth1, "+", "-"*note_colwidth2, "+"]))
  1506.  
  1507.             elif format == 'space':
  1508.                 log.info("")
  1509.  
  1510.         for l in links:
  1511.             log.info("\n.. _`%s`: %s.html\n" % (l, l.replace('hp-', '')))
  1512.  
  1513.         log.info("")
  1514.  
  1515.     elif typ == 'man':
  1516.         log.info('.TH "%s" 1 "%s" Linux "User Manuals"' % (crumb, version))
  1517.         log.info(".SH NAME\n%s \- %s" % (crumb, title))
  1518.  
  1519.         for line in text_list:
  1520.             text1, text2, format, trailing_space = line
  1521.  
  1522.             text1 = text1.replace("\\*", "*")
  1523.             text2 = text2.replace("\\*", "*")
  1524.  
  1525.             len1, len2 = len(text1), len(text2)
  1526.  
  1527.             if format == 'summary':
  1528.                 log.info(".SH SYNOPSIS")
  1529.                 log.info(".B %s" % text1.replace('Usage:', ''))
  1530.  
  1531.             elif format == 'name':
  1532.                 log.info(".SH DESCRIPTION\n%s" % text1)
  1533.  
  1534.             elif format in ('option', 'example', 'note'):
  1535.                 if text1:
  1536.                     log.info('.IP "%s"\n%s' % (text1, text2))
  1537.                 else:
  1538.                     log.info(text2)
  1539.  
  1540.             elif format in ('header', 'heading'):
  1541.                 log.info(".SH %s" % text1.upper().replace(':', '').replace('[', '').replace(']', ''))
  1542.  
  1543.             elif format in ('seealso, para'):
  1544.                 log.info(text1)
  1545.  
  1546.         log.info(".SH AUTHOR")
  1547.         log.info("HPLIP (Hewlett-Packard Linux Imaging and Printing) is an")
  1548.         log.info("HP developed solution for printing, scanning, and faxing with")
  1549.         log.info("HP inkjet and laser based printers in Linux.")
  1550.  
  1551.         log.info(".SH REPORTING BUGS")
  1552.         log.info("The HPLIP Launchpad.net site")
  1553.         log.info(".B https://launchpad.net/hplip")
  1554.         log.info("is available to get help, report")
  1555.         log.info("bugs, make suggestions, discuss the HPLIP project or otherwise")
  1556.         log.info("contact the HPLIP Team.")
  1557.  
  1558.         log.info(".SH COPYRIGHT")
  1559.         log.info("Copyright (c) 2001-9 Hewlett-Packard Development Company, L.P.")
  1560.         log.info(".LP")
  1561.         log.info("This software comes with ABSOLUTELY NO WARRANTY.")
  1562.         log.info("This is free software, and you are welcome to distribute it")
  1563.         log.info("under certain conditions. See COPYING file for more details.")
  1564.  
  1565.         log.info("")
  1566.  
  1567.  
  1568. def log_title(program_name, version, show_ver=True): # TODO: Move to base/module/Module class
  1569.     log.info("")
  1570.  
  1571.     if show_ver:
  1572.         log.info(log.bold("HP Linux Imaging and Printing System (ver. %s)" % prop.version))
  1573.     else:
  1574.         log.info(log.bold("HP Linux Imaging and Printing System"))
  1575.  
  1576.     log.info(log.bold("%s ver. %s" % (program_name, version)))
  1577.     log.info("")
  1578.     log.info("Copyright (c) 2001-9 Hewlett-Packard Development Company, LP")
  1579.     log.info("This software comes with ABSOLUTELY NO WARRANTY.")
  1580.     log.info("This is free software, and you are welcome to distribute it")
  1581.     log.info("under certain conditions. See COPYING file for more details.")
  1582.     log.info("")
  1583.  
  1584.  
  1585. def ireplace(old, search, replace):
  1586.     regex = '(?i)' + re.escape(search)
  1587.     return re.sub(regex, replace, old)
  1588.  
  1589.  
  1590. def su_sudo():
  1591.     su_sudo_str = None
  1592.  
  1593.     if which('kdesu'):
  1594.         su_sudo_str = 'kdesu -- %s'
  1595.  
  1596.     elif utils.which('/usr/lib/kde4/libexec/kdesu'):
  1597.         su_sudo_str = '/usr/lib/kde4/libexec/kdesu -- %s'
  1598.  
  1599.     elif utils.which('kdesudo'):
  1600.         su_sudo_str = 'kdesudo -- %s'
  1601.  
  1602.     elif which('gnomesu'):
  1603.         su_sudo_str = 'gnomesu -c "%s"'
  1604.  
  1605.     elif which('gksu'):
  1606.         su_sudo_str = 'gksu "%s"'
  1607.     
  1608.     elif which('su'):
  1609.         su_sudo_str = 'su'
  1610.  
  1611.     return su_sudo_str
  1612.  
  1613.  
  1614. #
  1615. # Removes HTML or XML character references and entities from a text string.
  1616. #
  1617.  
  1618. def unescape(text):
  1619.     def fixup(m):
  1620.         text = m.group(0)
  1621.         if text[:2] == "&#":
  1622.             # character reference
  1623.             try:
  1624.                 if text[:3] == "&#x":
  1625.                     #return unichr(int(text[3:-1], 16))
  1626.                     return chr(int(text[3:-1], 16))
  1627.                 else:
  1628.                     #return unichr(int(text[2:-1]))
  1629.                     return chr(int(text[2:-1]))
  1630.             except ValueError:
  1631.                 pass
  1632.         else:
  1633.             # named entity
  1634.             try:
  1635.                 #text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
  1636.                 text = chr(htmlentitydefs.name2codepoint[text[1:-1]])
  1637.             except KeyError:
  1638.                 pass
  1639.         return text # leave as is
  1640.     return re.sub("&#?\w+;", fixup, text)
  1641.  
  1642.  
  1643. # Adds HTML or XML character references and entities from a text string
  1644.  
  1645. def escape(s):
  1646.     if not isinstance(s, unicode):
  1647.         s = unicode(s) # hmmm...
  1648.  
  1649.     s = s.replace(u"&", u"&")
  1650.  
  1651.     for c in htmlentitydefs.codepoint2name:
  1652.         if c != 0x26: # exclude &
  1653.             s = s.replace(unichr(c), u"&%s;" % htmlentitydefs.codepoint2name[c])
  1654.  
  1655.     for c in range(0x20) + range(0x7f, 0xa0):
  1656.         s = s.replace(unichr(c), u"&#%d;" % c)
  1657.  
  1658.     return s
  1659.